﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Xml.Linq;
using VQWEB.administrator.sources;
using VQData;
using System.Data;
using VQWEB.common;


namespace VQWEB.administrator.membership
{
    public partial class role_add : System.Web.UI.Page
    {
        MembershipData MembershipData = new MembershipData();
        int Id = 0;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (RouteData.Values["Id"] != null)
                int.TryParse(RouteData.Values["Id"].ToString(), out Id);

            if (Id > 0)
            {
                frmRoles.DefaultMode = FormViewMode.Edit;
                core_user_role table = MembershipData.GetRolesById(Id);
                frmRoles.DataSource = new List<core_user_role> { table };
                frmRoles.DataBind();

            }
            else
                frmRoles.DefaultMode = FormViewMode.Insert;

            DataTable obj = ReadXML.Modules();
            ListView lvModules = HelperData.FindControlRecursive(this.Master, "lvModules") as ListView;
            lvModules.DataSource = obj;
            lvModules.DataBind();


        }

        protected void lvModules_ItemDataBound(object sender, ListViewItemEventArgs e)
        {
            Literal ltrItem;
            if (e.Item.ItemType == ListViewItemType.DataItem)
            {
                DataRowView rowView = (DataRowView)e.Item.DataItem;
                string r = rowView["name"].ToString();
                string scheck = MembershipData.CheckModuleInRole(Id, r) ? " checked" : "";
                ltrItem = (Literal)e.Item.FindControl("ltrItem");
                ltrItem.Text = "<input" + scheck + " type=\"checkbox\" name=\"module[]\" value=" + r + " ></input>";

            }

        } 
    }
}